import Request from 'utils/request';

export default class CommonApi {
  static getAddressByLatLng(latLng) {
    const apiName = 'geocoder/v1/';
    return Request.postMapService(apiName, {
      location: `${latLng.lat},${latLng.lng}`
    }).then(res => {
      return res.address;
    });
  }

  static getProvinceList() {
    const apiName = 'api/aprovinces/all';
    return Request.post(apiName).then(res => {
      let provinceList = res.data;
      provinceList = _.map(provinceList, province => {
        return {
          code: province.code,
          name: province.name
        };
      });
      return provinceList;
    });
  }

  static getCityList(code) {
    const apiName = 'api/acities/all';
    return Request.post(apiName, {
      provinceCode: code
    }).then(res => {
      let cityList = res.data;
      cityList = _.map(cityList, city => {
        return {
          code: city.code,
          name: city.name
        };
      });
      return cityList;
    });
  }

  static getAreaList(code) {
    const apiName = 'api/aareas/all';
    return Request.post(apiName, {
      cityCode: code
    }).then(res => {
      let areaList = res.data;
      areaList = _.map(areaList, area => {
        return {
          code: area.code,
          name: area.name
        };
      });
      return areaList;
    });
  }

  static getStreetList(code) {
    const apiName = 'api/astreets/all';
    return Request.post(apiName, {
      areaCode: code
    }).then(res => {
      let streetList = res.data;
      streetList = _.map(streetList, street => {
        return {
          code: street.code,
          name: street.name
        };
      });
      return streetList;
    });
  }

  static getVillageList(code) {
    const apiName = 'api/avillages/all';
    return Request.post(apiName, {
      streetCode: code
    }).then(res => {
      let villageList = res.data;
      villageList = _.map(villageList, village => {
        return {
          code: village.code,
          name: village.name
        };
      });
      return villageList;
    });
  }

  static sendVerificationCode(phone, hasLogin = true) {
    const apiName = 'api/phone/sendSms';

    return Request.post(apiName, {
      phone: phone
    }, true, hasLogin).then(res => {
      return res.msg;
    });
  }
}
